/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlasm.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.tanesha.recaptcha.ReCaptcha;
import net.tanesha.recaptcha.ReCaptchaFactory;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
import org.apache.struts2.ServletActionContext;
import org.w3c.dom.Document;
import utils.SimpleJSonObject;
import utils.XMLUtils;
import xmlasm.bo.UserBO;
import xmlasm.dao.UserDAO;
import xmlasm.dto.User;
import xmlasm.sync.FilePathHolder;

/**
 *
 * @author Thuc
 */
public class RegisterAction extends ActionSupport {

    private String username;
    private String password;
    private String confirm;
    private String fullname;
    private String email;
    private String challenge;
    private String uresponse;
    private InputStream inputStream;

    public String begin() throws Exception {
        ReCaptcha c = ReCaptchaFactory.newReCaptcha("6LfhTeISAAAAAC8t6oLctAQra66WynXELANxPE1Z", "6LfhTeISAAAAAC_YlkMcffrahm_uz67x4KOgF7Z6", false);
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("CAPTCHA", c.createRecaptchaHtml(null, null));

        return SUCCESS;
    }

    public String execute() throws Exception {
        SimpleJSonObject resultObject = new SimpleJSonObject();

        String userFilePath = FilePathHolder.getInstance().getUserXMLPath();
        Document doc = XMLUtils.parseDom(userFilePath);

        if (UserBO.checkDuplicateUsername(username, doc) && UserBO.checkDuplicateEmail(email, doc)) {
            resultObject.put("message", "failureall");
            resultObject.put("info", "Đã có người khác sử dụng.");
        } else if (UserBO.checkDuplicateUsername(username, doc)) {
            resultObject.put("message", "failureusername");
            resultObject.put("info", "Đây là username đã được sử dụng.");
        } else if (UserBO.checkDuplicateEmail(email, doc)) {
            resultObject.put("message", "failureemail");
            resultObject.put("info", "Đây là email đã được sử dụng.");
        } else {
            HttpServletRequest request = ServletActionContext.getRequest();
            String remoteAddr = request.getRemoteAddr();
            ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
            reCaptcha.setPrivateKey("6LfhTeISAAAAAC_YlkMcffrahm_uz67x4KOgF7Z6");
            ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
            if (!reCaptchaResponse.isValid()) {
                resultObject.put("message", "failurecaptcha");
                resultObject.put("info", "Mã xác nhận sai. Vui lòng nhập lại hoặc bấm refresh để nhận mã khác.");
            } else {
                int result = UserDAO.insertAccount(this);
                if (result > 0) {
                    User user = new User();
                    user.setUserId(result);
                    user.setUsername(username);
                    user.setFullName(fullname);
                    user.setPassword(password);
                    user.setEmail(email);
                    user.setIsAdmin(false);
                    user.setIsActive(true);
                    UserBO.insertUserToXML(user);
                    Map session = ActionContext.getContext().getSession();
                    session.put("USER", user);
                    resultObject.put("message", "success");
                }
            }
        }
        inputStream = new ByteArrayInputStream(resultObject.toString().getBytes("UTF-8"));
        return SUCCESS;
    }

    public String getConfirm() {
        return confirm;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getChallenge() {
        return challenge;
    }

    public void setChallenge(String challenge) {
        this.challenge = challenge;
    }

    public String getUresponse() {
        return uresponse;
    }

    public void setUresponse(String uresponse) {
        this.uresponse = uresponse;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
